package com.jieshao.setting

import com.intellij.mock.MockProject
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.impl.ApplicationImpl
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.EditorSettings
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.util.Disposer
import com.intellij.testFramework.Timings
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.testFramework.registerServiceInstance
import com.intellij.util.pico.DefaultPicoContainer
import com.jieshao.service.LineShareRuntime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.mockito.kotlin.*
import java.awt.BorderLayout
import javax.swing.JFrame
import javax.swing.JTextArea

class UIMockDebug : BasePlatformTestCase() {

//    @Test
//    @Disabled("仅手动运行")
    fun testFrame() {
//        SwingUtilities.invokeLater {
            // 创建一个 JFrame
            val frame = JFrame("Project Settings Test")
            frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
            frame.setSize(800, 600)
            // 创建 PicoContainer
            Timings.getStatistics()
//            val testAppManager = TestApplicationManager.getInstance()
            val picoContainer = DefaultPicoContainer()
            val projectDisposable = Disposer.newDisposable()
            val scope = CoroutineScope(Dispatchers.Default)
            // 创建一个模拟的 Project 对象
            val project = MockProject(picoContainer, projectDisposable)
            project.registerService(LineShareRuntime::class.java, mock<LineShareRuntime>())
            project.registerService(ProjectSettings::class.java, mock<ProjectSettings>())

            var applicationImpl = ApplicationImpl(scope, true)
            applicationImpl.registerServiceInstance(ApplicationSettings::class.java, mock<ApplicationSettings>())
            project.registerService(ApplicationManager::class.java, spy<ApplicationManager>())
            ApplicationManager.setApplication(
                applicationImpl
            )
            var document = mock<Document>()
            var setting = mock<EditorSettings>()
            var editor = mock<EditorEx> {
                    on {
                            getSettings()
                    } doReturn setting

                    on {
                            component
                    } doReturn JTextArea()
            }
            // 使用 Mockito 创建一个模拟的 EditorFactory
            val mockEditorFactory: EditorFactory = mock<EditorFactory> {
                    on {
                            createDocument(anyVararg(CharSequence::class))
                    } doReturn document

                    on {
                            createEditor(any(),any())
                    } doReturn editor
            }
            // 注册模拟的 EditorFactory 服务
            applicationImpl.registerServiceInstance(EditorFactory::class.java, mockEditorFactory)

            // 创建 ProjectSettingsConfigurable 实例
            val configurable = ProjectSettingsConfigurable(project)

            // 获取并设置组件
            val component = configurable.createPanel()
            frame.contentPane.add(component, BorderLayout.CENTER)

            // 显示 JFrame
            frame.isVisible = true

            while (frame.isVisible) {

            }
//        }
    }
}